/*
 * TowerGreenTwo.java
 *
 * Created on December 16, 2007, 7:22 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package hextd.tower;

import hextd.hex.Hex;
import hextd.creep.Creep;
import hextd.core.Context;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;

/**
 *
 * @author hylke
 */
public class TowerGreenTwo extends TowerBase {

	public static int price = 5000;
	public static int damage = 1000;
	public static float range = 4.25f;
	private Creep[] fireAt = {null, null};
	private float[] enemyX = {0, 0};
	private float[] enemyY = {0, 0};
	private float rJump = 2.5f;
	private Creep.COLOR damageColor = Creep.COLOR.GREEN;
	private Color lineColorDark;

	/** Creates a new instance of TowerGreenTwo */
	public TowerGreenTwo(Context context, Hex hex, int owner, int activateTick) {
		super(TowerFactory.type.greenTwo, price, damage, range, owner, activateTick);
		this.lineColor = Color.GREEN;
		this.lineColorDark = new Color(this.lineColor.getRed() / 2, this.lineColor.getGreen() / 2, this.lineColor.getBlue() / 2);
		this.doInit(context, hex);
	}

	@Override
	public void runTick(int gameTime) {
		if (!this.lock) {
			this.fireAt[0] = null;
		}
		this.fireAt[1] = null;
		this.fireAt[0] = this.findEnemy(this.fireAt[0], false);
		if (this.fireAt[0] != null) {
			this.enemyX[0] = this.fireAt[0].getX();
			this.enemyY[0] = this.fireAt[0].getY();
			this.fireAt[0].doDamage(this.getOwner(), this.damageCurrent, this.damageColor);
			this.fireAt[1] = this.findEnemyClosest(this.enemyX[0], this.enemyY[0], this.rJump, this.fireAt);
			if (this.fireAt[1] != null) {
				this.enemyX[1] = this.fireAt[1].getX();
				this.enemyY[1] = this.fireAt[1].getY();
				this.fireAt[1].doDamage(this.getOwner(), this.damageCurrent, this.damageColor);
			}
		}
	}

	@Override
	public String getStatusString() {
		String retString = "";
		retString += "<h1>Green Tower 2</h1>" +
				"<p>" +
				"<b>Level:</b> " + (1 + this.level) + "<br>\n" +
				"<b>Damage:</b> " + this.damageText + "<br>\n" +
				"<b>Range:</b> " + this.rangeText + "<br>" +
				"</p>\n";
		return retString;
	}

	@Override
	public String getInfoString() {
		String retString = "";
		retString += "<h1>Green Tower 2</h1>" +
				"<p>" +
				"Tower with one medium laser that refracts from the first target to a second. Both targets receive full damage.<br>" +
				"Strong against green enemies, very weak against red enemies.</p>" +
				"<p>" +
				"<b>Price:</b> " + TowerGreenTwo.form.format(TowerGreenTwo.price) + "<br>" +
				"<b>Damage:</b> " + TowerGreenTwo.form.format(this.damageCurrent) + "<br>" +
				"<b>Range:</b> " + TowerGreenTwo.form.format(rangeCurrent) + "<br>" +
				"<b>Refire rate:</b> 20/s" +
				"</p>";
		return retString;
	}

	@Override
	protected void generateShapes() {
		float width = hexPaintWidth;
		int shapeCount = 6;
		this.shapes = new Shape[shapeCount];
		this.shapes[0] = new Ellipse2D.Float(-0.5f * this.paintScale, -0.5f * this.paintScale, 1.0f * this.paintScale, 1.0f * this.paintScale);
		this.shapes[1] = new Ellipse2D.Float(-0.25f * this.paintScale, -0.25f * this.paintScale, 0.5f * this.paintScale, 0.5f * this.paintScale);
		this.shapes[2] = new Line2D.Float(0, 0.5f * paintScale, 0, paintScale);
		this.shapes[3] = new Line2D.Float(-0.5f * width, -0.5f * 0.5f * paintScale, -width, -0.5f * paintScale);
		this.shapes[4] = new Line2D.Float(0.5f * width, -0.5f * 0.5f * paintScale, width, -0.5f * paintScale);
		this.shapes[5] = new Ellipse2D.Float(-1.0f * strokeScale, -1.0f * strokeScale, 2.0f * strokeScale, 2.0f * strokeScale);

		this.shapeAction = new int[shapeCount];
		this.shapeAction[0] = 1;
		this.shapeAction[1] = 1;
		this.shapeAction[2] = 1;
		this.shapeAction[3] = 1;
		this.shapeAction[4] = 1;
		this.shapeAction[5] = 1;

		this.shapeColor = new Color[shapeCount];
		this.shapeColor[0] = this.lineColor;
		this.shapeColor[1] = this.lineColorDark;
		this.shapeColor[2] = this.lineColor;
		this.shapeColor[3] = this.lineColor;
		this.shapeColor[4] = this.lineColor;
		this.shapeColor[5] = this.lineColor;

		this.strokes = new Stroke[shapeCount];
		this.strokes[0] = new BasicStroke(2.0f * strokeScale, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL);
		this.strokes[1] = this.strokes[0];
		this.strokes[2] = this.strokes[0];
		this.strokes[3] = this.strokes[0];
		this.strokes[4] = this.strokes[0];
		this.strokes[5] = this.strokes[0];
	}

	@Override
	public void paintEffects(Graphics2D g2, int gameTime) {
		Stroke defaultStroke = g2.getStroke();
		Creep fa0 = fireAt[0];
		Creep fa1 = fireAt[1];
		if (fa0 != null) {
			g2.setColor(this.lineColor);
			float e1x = fa0.getPaintX() + ((float) (Math.random() - 0.5) * this.paintScale / 10f);
			float e1y = fa0.getPaintY() + ((float) (Math.random() - 0.5) * this.paintScale / 10f);
			g2.draw(new Line2D.Float(this.xPaint, this.yPaint, e1x, e1y));
			if (fa1 != null) {
				float e2x = fa1.getPaintX() + ((float) (Math.random() - 0.5) * this.paintScale / 10f);
				float e2y = fa1.getPaintY() + ((float) (Math.random() - 0.5) * this.paintScale / 10f);
				g2.draw(new Line2D.Float(e2x, e2y, e1x, e1y));
			}
		}

		g2.setStroke(defaultStroke);
	}
}
